home *** CD-ROM | disk | FTP | other *** search
/ A.C.E. 2 / ACE CD 2.iso / FILES / UTILS / AMOSPRO4.DMS / in.adf / Tutorials / Collision_Detection.AMOS / Collision_Detection.amosSourceCode < prev    next >
Encoding:
AMOS Source Code  |  1992-09-28  |  8.0 KB  |  222 lines

  1. '*************************** 
  2. '*    AMOS Professional    *             INSTRUCTIONS COVERED
  3. '*                         * 
  4. '*   COLLISION DETECTION   *                Sprite Col 
  5. '*                         *                Bob Col
  6. '* (c) Europress Software  *                Spritebob Col  
  7. '*                         *                Bobsprite Col
  8. '*     Ronnie Simpson      *                Col
  9. '***************************                Make mask
  10. '
  11. '------------------------------------------- 
  12. 'DETECTING COLLISIONS
  13. '------------------------------------------- 
  14. 'There are two stages involved in finding exactly which two objects have 
  15. 'collided. 
  16. '
  17. 'STAGE 1 
  18. '
  19. 'First you test an individual sprite or bob using one of the four functions
  20. 'provided:-
  21. '
  22. '      
  23. '          Sprite Col    (test for a sprite -> sprite collision) 
  24. '          Bob Col       (test for a bob -> bob collision) 
  25. '          Spritebob Col (test for a sprite -> bob collision)
  26. '          Bobsprite Col (test for a bob -> sprite collision)
  27. '
  28. 'Each of these functions use the same syntax and there is a normal and 
  29. 'expanded form of each 
  30. '
  31. '       normal- Sprite Col (1)            Bobsprite Col(2) 
  32. '     expanded- Sprite Col(1,5 To 7)      Bobsprite Col(2,5 To 7)  
  33. '
  34. 'Normal version tests if the named sprite or bob has collided with ANY object  
  35. 'of the type tested. 
  36. '
  37. 'Expanded version restricts the test to the stated series. 
  38. '
  39. 'eg.   
  40. '    C=Bob Col(4)   (test if bob number 4 has collided with any other bobs)    
  41. '
  42. '    C=Spritebob Col(2)   (test if sprite 2 has collided with any bobs)    
  43. '
  44. '    C=Sprite Col(1,2 To 7)    (test if sprite 2 has collided with any of)     
  45. '                              (      the sprites numberd 2 -> 7         ) 
  46. '
  47. '    C=Bobsprite Col(1,3 To 5) (test if bob 1 has collided with any of)      
  48. '                              (     the sprites numberd 3,4 or 5     )
  49. '
  50. 'If a collision has occured then a value of -1 (or TRUE), otherwise the  
  51. 'result will be 0 (or FALSE) 
  52. '
  53. 'STAGE 2 
  54. '
  55. 'To find exactly which object your tested bob or sprite has collided with
  56. 'the function =Col is used.
  57. '
  58. 'This function should be used as soon as possible after the original test to 
  59. 'avoid any confusion.
  60. '
  61. 'It works in a similar manner as an array with each bob or sprite being
  62. 'associated with an element in this array according to its identification
  63. 'number. 
  64. '
  65. 'If a collision has occured then a value of -1 (or TRUE) will be returned
  66. 'otherwise the result will be 0 (or FALSE).  
  67. '
  68. 'If a negative number is used as the parameter for the col() function then 
  69. 'an automatic test will be performed and the number returned will be the 
  70. 'first bob or sprite which has collided, the only restriction with this is 
  71. 'that sprite number 0 can not be tested with this version of the function.   
  72. '
  73. 'Let's say for example you were writing a space invaders game with the 
  74. 'objects numbered as follows:- 
  75. '
  76. '             gun = Sprite 0             
  77. '           shell = Sprite 7   
  78. '       48 aliens = bobs numbered 1-48   
  79. '
  80. 'To check for collisions between the shell and the aliens then something 
  81. 'like this could be used:- 
  82. '
  83. '         If Spritebob Col(7,1 To 48) Then C=Col(-1):Proc RUBOUT[C]  
  84. '             :     :
  85. '             :     :
  86. '         Procedure RUBOUT[N]
  87. '           Bob Off N : Boom 
  88. '         End Proc 
  89. '
  90. '------------------------------------------- 
  91. 'FINAL NOTES 
  92. '------------------------------------------- 
  93. 'Collision detection routines rely on a sprite mask being present, if a call 
  94. 'to No Mask has been made then collisions can not be detected. 
  95. '
  96. 'If you are using Bob Col or Spritebob Col then the results by the =Col  
  97. 'function will be bobs otherwise they will be sprites, therefore it makes  
  98. 'sense to use the =Col function as soon as possible after the original test  
  99. 'to avoid any confusion. 
  100. '
  101. '------------------------------------------- 
  102. 'WORKING EXAMPLE 
  103. '------------------------------------------- 
  104. Dim X(20),Y(20),DX(20),DY(20)
  105. Global X(),Y(),DX(),DY()
  106. Load "AMOSPro_Tutorial:Objects/Col.abk" : Make Mask 
  107. Screen Open 0,640,200,4,Hires
  108. Flash Off : Curs Off : Palette $30,$5F5,$3 : Cls 0 : Pen 1 : Paper 2
  109. Reserve Zone 4
  110. Locate 0,5 : Centre Border$(Zone$(" Example 1 - Static bat with 1 ball ",1),4)
  111. Locate 0,10 : Centre Border$(Zone$(" Example 2- Moving bat with 1 ball + bricks ",2),4)
  112. Locate 0,15 : Centre Border$(Zone$(" Example 3- Moving bat with 8 balls ",3),4)
  113. Locate 0,20 : Centre Border$(Zone$(" Quit ",4),4)
  114. Do 
  115.    M=Mouse Zone
  116.    If Mouse Key and M=4 Then Edit 
  117.    If Mouse Key and M>0
  118.       On M Proc EXAMPLE1,EXAMPLE2,EXAMPLE3
  119.       Sprite Off : Limit Mouse : Show On 
  120.       Repeat : Until Mouse Key=0
  121.    End If 
  122. Loop 
  123. Procedure EXAMPLE1
  124.    Screen Open 1,320,200,8,Lowres
  125.    Get Sprite Palette : Colour 0,0 : Curs Off : Flash Off : Cls 0 : Hide On 
  126.    Set Paint 1 : Ink 5,7,4 : Bar 20,20 To 300,180 : Ink 7 : Box 21,21 To 299,179
  127.    Bob 1,160,110,1
  128.    Pen 7 : Paper 5 : Locate 0,20 : Centre "Press right mouse key to continue."
  129.    Pen 2 : Locate 0,4 : Centre "Example 1"
  130.    X=50 : Y=30 : DX=2 : DY=2
  131.    Double Buffer 
  132.    '
  133.    Do 
  134.       Exit If Mouse Key=2
  135.       Add X,DX : Add Y,DY
  136.       If X>294 or X<26 Then DX=-DX
  137.       If Y>174 or Y<26 Then DY=-DY
  138.       If Bob Col(1) Then Boom : DY=-DY : Add Y,DY*2
  139.       Bob 2,X,Y,2
  140.       Wait Vbl 
  141.    Loop 
  142.    '
  143.    Screen Close 1
  144. End Proc
  145. Procedure EXAMPLE2
  146.    Screen Open 1,320,200,8,Lowres : Double Buffer 
  147.    Get Sprite Palette : Colour 0,0 : Curs Off : Flash Off : Cls 0 : Hide On 
  148.    Set Paint 1 : Ink 5,7,4 : Bar 0,0 To 319,199 : Ink 7 : Box 1,1 To 318,198
  149.    Ink 7 : Draw 1,199 To 318,199
  150.    Pen 7 : Paper 5 : Locate 0,20 : Centre "Press right mouse key to continue"
  151.    Pen 2 : Locate 0,6 : Centre "Example 2"
  152.    X=40 : Y=40 : DX=2 : DY=2 : BALL=1 : COUNT=0
  153.    Ink 5 : Locate 0,14 : Centre "Ball number"+Str$(BALL)
  154.    Limit Mouse 175,150 To 445,150
  155.    For N=1 To 9 : Bob N+2,N*32+16,30,3 : Next : X(12)=160 : Bob 12,160,100,5
  156. '
  157.    Do 
  158.       Add X(12),3,-15 To 320 : Bob 12,X(12),,
  159.       Exit If Mouse Key=2
  160.       Bob 1,X Mouse-150,Y Mouse+40,1
  161.       Add X,DX : Add Y,DY
  162.       If X>312 or X<6 Then DX=-DX
  163.       If Y<6 Then DY=-DY
  164.       If Y>196 Then Boom : Y=8 : Inc BALL : Locate 24,14 : Print Str$(BALL)
  165.       If Bob Col(2)
  166.          C=Col(-1)
  167.          If C=1
  168.             Y=180 : DY=-DY : Shoot 
  169.          End If 
  170.          If C=12
  171.             DY=-DY : Bell 1 : DX=Rnd(8)-4 : DY=-DY
  172.          End If 
  173.          If C>2 and C<12
  174.             Bob C,500,, : Inc COUNT : DY=-DY : Shoot 
  175.             If COUNT=9
  176.                COUNT=0 : For N=1 To 9 : Bob N+2,N*32+16,30,3 : Next 
  177.             End If 
  178.          End If 
  179.       End If 
  180.       Bob 2,X,Y,2
  181.       Wait Vbl 
  182. '
  183.    Loop 
  184.    Screen Close 1
  185. End Proc
  186. Procedure EXAMPLE3
  187.    Screen Open 1,320,200,8,Lowres : Double Buffer 
  188.    Get Sprite Palette : Colour 0,0 : Curs Off : Flash Off : Cls 0 : Hide On 
  189.    Colour 17,0 : Colour 21,0 : Colour 25,0 : Colour 29,0
  190.    Colour 18,$F : Colour 22,$F0 : Colour 26,$F00 : Colour 30,$FF0
  191.    Colour 19,$F : Colour 23,$F0 : Colour 27,$F00 : Colour 31,$FF0
  192.    Set Paint 1 : Ink 5,7,4 : Bar 0,0 To 319,199 : Ink 7 : Box 1,1 To 318,198
  193.    Ink 7 : Draw 1,199 To 318,199 : Ink 2 : Bar 2,188 To 317,199
  194.    Pen 7 : Paper 5 : Locate 0,20 : Centre "Press right mouse key to continue"
  195.    Pen 2 : Locate 0,6 : Centre "Example 3"
  196.    Pen 3 : Locate 7,15 : Print "Energy-"
  197.    For N=0 To 7
  198.       X(N)=Rnd(280)+160 : Y(N)=Rnd(160)+60
  199.       Repeat 
  200.          DX(N)=Rnd(6)-3 : DY(N)=Rnd(6)-3
  201.       Until DX(N)<>0 and DY(N)<>0
  202.    Next 
  203.    NRG=80 : Autoback 1
  204.    Ink 6 : Box 119,122 To 201,124 : Ink 1 : Draw 120,123 To NRG+120,123
  205.    Limit Mouse 175,150 To 445,150 : Ink 0 : Timer=0
  206. '
  207.    Do 
  208.       Exit If Mouse Key=2 or NRG<1
  209.       Bob 1,X Mouse-150,Y Mouse+40,1
  210.       For N=0 To 7
  211.          Add X(N),DX(N) : Add Y(N),DY(N)
  212.          Sprite N,X(N),Y(N),4
  213.          If X(N)>442 or X(N)<134 Then DX(N)=-DX(N)
  214.          If Y(N)<56 Then DY(N)=-DY(N)
  215.          If Y(N)>234 Then Boom : Add NRG,-3 : Plot 121+NRG,123 : DY(N)=-DY(N)
  216.       Next 
  217.       If Bobsprite Col(1,0 To 7) Then Bell 10 : C=Col(-1) : DY(C)=-DY(C) : Y(C)=230
  218.    Loop 
  219. '
  220.    If NRG<1 Then Pen 6 : Locate 0,18 : Centre "GAME OVER - you lasted"+Str$(Timer/50)+" seconds" : Repeat : Until Mouse Key
  221. Screen Close 1
  222. End Proc